package sk.tramtatiri.data.dao.jpa;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import org.springframework.orm.jpa.JpaCallback;
import org.springframework.transaction.annotation.Transactional;

import sk.tramtatiri.data.dao.interfaces.InOutHelperDao;
import sk.tramtatiri.data.dataobjects.InOutHelper;

public class InOutHelperDaoJPAImp extends AbstractDaoJPAImpl<InOutHelper>
		implements InOutHelperDao {

	public InOutHelperDaoJPAImp() {
		super(InOutHelper.class);
	}

	public List<InOutHelper> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	public int countAll() {
		// TODO Auto-generated method stub
		return 0;
	}

	public InOutHelper findName(String name) {
		// TODO Auto-generated method stub
		return null;
	}

	@Transactional
	public InOutHelper findByUserToday(final long user_id) {
		Calendar c = Calendar.getInstance();
		c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH),
				c.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		final Date date = new Date(c.getTimeInMillis());
		try {
			return getJpaTemplate().execute(new JpaCallback<InOutHelper>() {
				public InOutHelper doInJpa(EntityManager em)
						throws PersistenceException {
					TypedQuery<InOutHelper> query = em
							.createQuery(
									"select h from InOutHelper h where h.usrid = :userId and h.date = :date",
									InOutHelper.class);
					query.setParameter("userId", user_id);
					query.setParameter("date", date);
					return query.getSingleResult();
				}
			});
		} catch (Exception e) {
			return null;
		}
	}
	
	@Transactional
	public List<Object[]> findByUserLastXDays(final long user_id, final int days) {
		Calendar c = Calendar.getInstance();
		c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH),
				c.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		c.add(Calendar.DAY_OF_MONTH, -days);
		final Date date = new Date(c.getTimeInMillis());

		return getJpaTemplate().execute(new JpaCallback<List<Object[]>>() {
			public List<Object[]> doInJpa(EntityManager em)
					throws PersistenceException {
				Query query0 = null;
				if (days < 99) {
					query0 = em
							.createQuery("select h.date,h.caloriesIn,h.caloriesOut from InOutHelper h where h.usrid = :userId and h.date >= :startDate order by h.date asc");
					query0.setParameter("startDate", date);
				} else {
					query0 = em
							.createQuery("select h.date,h.caloriesIn,h.caloriesOut from InOutHelper h where h.usrid = :userId order by h.date asc");
				}
				query0.setParameter("userId", user_id);
				return query0.getResultList();
			}
		});
	}

	public List<Date> findLast30Dates(final long user_id) {
		return getJpaTemplate().execute(new JpaCallback<List<Date>>() {
			public List<Date> doInJpa(EntityManager em)
					throws PersistenceException {
					TypedQuery<Date> query0 = em
							.createQuery("select h.date from InOutHelper h where h.usrid = :userId order by h.date asc limit 30",Date.class);
				query0.setParameter("userId", user_id);
				return query0.getResultList();
			}
		});
	}

	@Transactional
	public void delete(InOutHelper o) {
		InOutHelper h = getJpaTemplate().find(InOutHelper.class, o.getId());
		getJpaTemplate().remove(h);
	}

}
